package com.lancoo.znbkxx.teacher.diagnosis

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.view.View
import com.github.mikephil.charting.charts.LineChart
import com.github.mikephil.charting.components.MarkerView
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.data.Entry
import com.github.mikephil.charting.data.LineData
import com.github.mikephil.charting.data.LineDataSet
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter
import com.github.mikephil.charting.highlight.Highlight
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.base.BaseFragment
import com.lancoo.znbkxx.teacher.model.data.LanguageTrail
import kotlinx.android.synthetic.main.fragment_language_trail.*
import kotlinx.android.synthetic.main.item_achievement_markview.view.*
import java.math.BigDecimal


/**
 *@author Admin
 *@date 2021/1/7
 *
 */
class LanguageTrailFragment : BaseFragment() {
    private val skillExcellent = mutableListOf<Entry>()
    private val skillTotal = mutableListOf<Entry>()
    private val skillPersonal = mutableListOf<Entry>()
    private val efficiencyExcellent = mutableListOf<Entry>()
    private val efficiencyTotal = mutableListOf<Entry>()
    private val efficiencyPersonal = mutableListOf<Entry>()
    private val xLabels = mutableListOf<String>()
    private val trailInfo by lazy { arguments?.getParcelable<LanguageTrail>("trail") }
    override fun createLayout(): Int {
        return R.layout.fragment_language_trail
    }

    override fun initView(view: View?) {
        if (trailInfo!!.CognitiveAbility.isEmpty()) {
            group.visibility = View.GONE
            tv_empty_a.visibility = View.VISIBLE
        } else {
            initLineChart(lc_ability, 0, trailInfo!!)
            group.visibility = View.VISIBLE
            tv_empty_a.visibility = View.GONE
        }
        if (trailInfo!!.LearningEfficiency.isEmpty()) {
            group3.visibility = View.GONE
            tv_empty_b.visibility = View.VISIBLE
        } else {
            initLineChart(lc_efficiency, 1, trailInfo!!)
            group3.visibility = View.VISIBLE
            tv_empty_b.visibility = View.GONE
        }
    }


    private fun initLineChart(lineChart: LineChart, type: Int, trailInfo: LanguageTrail) {
        xLabels.clear()
        if (type == 0) {
            skillExcellent.clear()
            skillTotal.clear()
            skillPersonal.clear()
            trailInfo.CognitiveAbility.forEachIndexed { index, item ->
                val entryExcellent = Entry(index.toFloat(), item.ExcellentSkillScore.toFloat())
                val entryTotal = Entry(index.toFloat(), item.TotalSkillScore.toFloat())
                val entryPersonal = Entry(index.toFloat(), item.PerSkillScore.toFloat())
                skillExcellent.add(entryExcellent)
                skillTotal.add(entryTotal)
                skillPersonal.add(entryPersonal)
                xLabels.add(item.DayTime)
            }
        } else {
            efficiencyExcellent.clear()
            efficiencyTotal.clear()
            efficiencyTotal.clear()
            trailInfo.LearningEfficiency.forEachIndexed { index, item ->
                val entryExcellent = Entry(index.toFloat(), item.ExcellentEfficiency.toFloat())
                val entryTotal = Entry(index.toFloat(), item.TotalEfficiency.toFloat())
                val entryPersonal = Entry(index.toFloat(), item.PerEfficiency.toFloat())
                efficiencyExcellent.add(entryExcellent)
                efficiencyTotal.add(entryTotal)
                efficiencyPersonal.add(entryPersonal)
                xLabels.add(item.DayTime)
            }
        }
        lineChart.apply {
            val dataSetExcellent = LineDataSet(
                    if (type == 0) skillExcellent else efficiencyExcellent, "excellent")
            dataSetExcellent.apply {
                isHighlightPerTapEnabled = false
                color = Color.parseColor("#22d7bb")
                setCircleColor(Color.parseColor("#22d7bb"))
                lineWidth = 2f
                circleRadius = 2.5f
                setDrawCircleHole(true)
                circleHoleRadius = 1.5f
            }
            val dataSetTotal = LineDataSet(if (type == 0) skillTotal else efficiencyTotal,
                    "total")
            dataSetTotal.apply {
                isHighlightEnabled = false
                color = Color.parseColor("#ffd234")
                setCircleColor(Color.parseColor("#ffd234"))
                lineWidth = 2f
                circleRadius = 2.5f
                setDrawCircleHole(true)
                circleHoleRadius = 1.5f
            }
            val dataSetPersonal = LineDataSet(
                    if (type == 0) skillPersonal else efficiencyPersonal, "personal")
            dataSetPersonal.apply {
                isHighlightEnabled = false
                color = Color.parseColor("#2dbcff")
                setCircleColor(Color.parseColor("#2dbcff"))
                lineWidth = 2f
                circleRadius = 2.5f
                setDrawCircleHole(true)
                circleHoleRadius = 1.5f
            }
            val lineData = LineData(dataSetExcellent, dataSetTotal, dataSetPersonal)
            lineData.setDrawValues(false)
            data = lineData
            axisLeft.apply {
                textColor = Color.parseColor("#999999")
                spaceBottom = 0f
                axisMinimum = 0f
                axisMaximum = 105f
            }
            axisRight.isEnabled = false
            legend.isEnabled = false
            description.isEnabled = false
            setPinchZoom(false)
            setScaleEnabled(false)
            xAxis.apply {
                axisMinimum = -0.5f
                axisMaximum = if (type == 0) {
                    trailInfo.CognitiveAbility.size.toFloat()
                } else {
                    trailInfo.LearningEfficiency.size.toFloat()
                } - 0.5f
                position = XAxis.XAxisPosition.BOTTOM
                isGranularityEnabled = true
                labelCount = xLabels.size
                setDrawGridLines(false)
                textSize = 11f
                textColor = Color.parseColor("#999999")
                valueFormatter = IndexAxisValueFormatter(xLabels)
            }
            val markerView = MyMarkView(requireContext(), R.layout.item_achievement_markview, type)
            markerView.chartView = this
            marker = markerView
            animateX(1000)
        }
    }

    inner class MyMarkView(context: Context, layoutResource: Int, val type: Int) :
            MarkerView(context, layoutResource) {
        @SuppressLint("SetTextI18n")
        override fun refreshContent(e: Entry?, highlight: Highlight?) {
            val position = e!!.x.toInt()
            tv_hint.text = "优秀学生：${if (type == 0) skillExcellent[position].y else efficiencyExcellent[position].y}\n" +
                    "整体学生：${if (type == 0) skillTotal[position].y else efficiencyTotal[position].y}\n" +
                    "学生个人：${if (type == 0) skillPersonal[position].y else efficiencyPersonal[position].y}"
            super.refreshContent(e, highlight)
        }
    }
}